package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 *
 * @author gzh
 * @date 2025-10-17
 */
//是 ​​@Controller​​ 和 ​​@ResponseBody​​ 这两个注解的组合体
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备管理的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    public AjaxResult queryProduct(@PathVariable String productKey){
        return deviceService.queryProduct(productKey);
    }

    /*
     * 查看设备上报的最新数据
     * */
    @ApiOperation("查看设备上报的最新数据")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/queryProductById/{productKey}")
    public AjaxResult queryProductById(@PathVariable("productKey") String productKey) {

        return deviceService.querySProductById(productKey);
    }

    /*
     * 编辑设备
     * */
    @ApiOperation("编辑设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return AjaxResult.success("设备编辑成功");
    }

    /*
     * 删除设备
     * */
    @ApiOperation("删除设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @DeleteMapping ("/{iotId}")
    public AjaxResult deleteDevice(@PathVariable("iotId") String iotId) {
        deviceService.deleteDevice(iotId);
        return AjaxResult.success("设备删除成功");
    }
    /*
     * 查看设备上报的最新数据
     * */
    @ApiOperation("查看设备上报的最新数据")
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProPerties(@PathVariable("iotId") String iotId) {

        List<DeviceDataVo> deviceDataVoList = deviceService.queryServiceProperties(iotId);
        return AjaxResult.success(deviceDataVoList);
    }

    /*
     * 查看设备详情
     * */
    @ApiOperation("查看设备详情")
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/{iotId}")
    public AjaxResult deviceDetail(@PathVariable("iotId") String iotId) {

        DeviceDetailVo deviceDetailVo = deviceService.deviceDetail(iotId);
        return AjaxResult.success(deviceDetailVo);
    }
    /*
     * 注册设备
     * */
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public AjaxResult register(@RequestBody DeviceDto dto) {

        //register:注册
        deviceService.register(dto);
        return AjaxResult.success();
    }

    /*
     * 查询所有产品列表
     * */
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> productVoList = deviceService.allProduct();
        return AjaxResult.success(productVoList);
    }


    /**
     * 从物联网平台同步产品列表
     */

    @PostMapping("/syncProductList")
    //方法执行前的权限认证, @ss- 权限服务Bean引用,指向Spring容器中名为"ss"的Bean实例,"ss"取自Spring Security的首字母缩写
    //nursing：模块名（护理模块）,device：资源名（设备资源）,list：操作类型（列表查询）
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询设备列表
     */

    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备列表")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

}